package com.cn.lf.wx.dacs.bussiness.author.serviceImpl;


import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cn.lf.wx.dacs.bussiness.author.bean.SimpleUser;
import com.cn.lf.wx.dacs.bussiness.author.utils.ClaimsUtils;
import com.cn.lf.wx.dacs.bussiness.author.utils.Constant;
import com.cn.lf.wx.dacs.bussiness.author.utils.JwtUtils;
import com.cn.lf.wx.dacs.bussiness.author.utils.StringUtil;
import com.cn.ym.log.Log;

import io.jsonwebtoken.Claims;

@Service("permission")
public class PermissionService {


    public boolean login() {
    	System.out.println("111");
        SimpleUser user = checkUser();
        
        return Objects.nonNull(user);
    }

    public SimpleUser checkUser() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();
        String token = request.getHeader(Constant.Setting.HEADER_AUTH);
        if (StringUtil.isEmpty(token)) {
            return null;
        }
        try {
            Claims claims = JwtUtils.parseJWT(token);
            SimpleUser authUser = ClaimsUtils.claims2User(claims);
            authUser.setToken(token);
            return authUser;
        } catch (Exception e) {
            Log.error("token 解析 {}"+ e.getMessage());
            return null;
        }
    }

}
